package com.icss.th.search.servlet;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.icss.th.db.pojo.PageInfo;
import com.icss.th.fulltext.FullTextSearchFactory;
import com.icss.th.fulltext.IFullTextSearch;
import com.icss.th.fulltext.pojo.SearchResult;

/**
 * Servlet implementation class SearchServlet
 */
@WebServlet("/search")
public class SearchServlet extends HttpServlet 
{
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		String keyword = URLDecoder.decode(request.getParameter("keyword"), "UTF-8");
		String subjectId = request.getParameter("subjectId");
		
		PageInfo pi = new PageInfo();
		
		List<SearchResult> list = null;
		long beginTime = new Date().getTime();
		IFullTextSearch search = FullTextSearchFactory.getInstance();
		if (search != null)
		{
			list = search.simpleSearch("0".equals(subjectId)? null : subjectId, keyword, pi);
		}
		float spendTime = (new Date().getTime() - beginTime)/1000f;
		
		request.setAttribute("list", list);
		request.setAttribute("pageInfo", pi);
		request.setAttribute("spendTime", String.valueOf(spendTime));
		
		request.setAttribute("keyword", keyword);
		request.setAttribute("subjectId", subjectId);
		
		request.getRequestDispatcher("/list.jsp").forward(request, response);
	}
}
